package edu.upenn.cis.cis550.hw3.server;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;


import edu.upenn.cis.cis550.hw3.shared.Relation;
import edu.upenn.cis.cis550.hw3.shared.Tuple;

public class DataExporter{
	    
	    QueryTablesImpl querying = new QueryTablesImpl();
	    QueryExportImpl queryingExport = new QueryExportImpl();
	    private StringBuilder xml = new StringBuilder();
	    
	    public static void main(String[] args) throws IOException, ClassNotFoundException {
            DataExporter.run();
		}
		
		private static void run() throws IOException, ClassNotFoundException {
		    DataExporter exp = new DataExporter();
		    System.out.println(exp.getAllUsersVisualizerXML());
		} 
	    
	    public String getAllUsersVisualizerXML() {
	        Iterator<Tuple> i = queryingExport.getAllUsersIDs().iterator();
	        xml.append("<photodb> \n");
	           while(i.hasNext()){
	        	   Tuple t = i.next();
	        	   String currentID = t.getValue("userID").toString();	
	        	   Boolean student = false;
	        	   if(querying.isItStudent(currentID)){
	        		   xml.append("  <user xsi:type=\"studentType\"> \n");
	        		   student = true;
	        	   } else {
	        		   xml.append("  <user xsi:type=\"professorType\"> \n");
	        	   }
	        	   xml.append("   <userID>");
                   xml.append(currentID);
                   xml.append("</userID>  \n");
                   Relation userInfo = querying.getUserInfo(currentID);
	        	   buildNode(userInfo, "");
	        	   Relation userAddress = queryingExport.getUsersAddress(currentID);
	        	   buildNode(userAddress, "address");
	        	   Relation userPassword = queryingExport.getUsersPassword(currentID);
	        	   buildNode(userPassword, "");
	        	   Relation userInterests = queryingExport.getUsersInterests(currentID);
	        	   //FIX INTEREST
	        	   buildNode(userInterests, "");
	        	   Relation userPhotos = queryingExport.getUsersPhotos(currentID);
	        	   buildNode(userPhotos, "photo");
	        	   Relation userCircles = queryingExport.getUsersCircles(currentID);
	        	   buildNode(userCircles, "circle");
	        	   Relation additInfo; 
	        	   if(student){
	        		   additInfo = querying.getStudentInfo(currentID);
	        	   } else {
	        		   additInfo = querying.getProfessorInfo(currentID);
	        	   }
	        	   buildNode(additInfo, "");
	           }
	            xml.append("  </user>  \n");        
	        xml.append("</photodb> \n");	                
	        return xml.toString();
	    }
	    
	    public void buildNode(Relation r, String root){
	    	Iterator<Tuple> it = r.iterator();
	    	 List<String> elements = r.getSchema().getNames();
	    	 Object[] tags = elements.toArray(); 
	    	 while(it.hasNext()){
	    		 if (!root.equals("")) 
	    			 xml.append("  <" + root + "> \n"); 
	    		 Tuple t = it.next();
	    		 for(int j = 0; j < tags.length; j++){
	    			 xml.append("  <" + tags[j] + ">");
	    			 xml.append(t.getValue(tags[j].toString()));
	    			 xml.append("</" + tags[j] + "> \n");
	    		 }	
    			 if(root.equals("photo")){
	    			   String currentID = t.getValue("photoID").toString();	 
	  	        	   Relation photosRatings = queryingExport.getPhotoRating(currentID);
		        	   buildNode(photosRatings, "rating");
		        	   Relation photosComments = queryingExport.getPhotoComments(currentID);
		        	   buildNode(photosComments, "comment");
	    	     }
    			 if(root.equals("circle")){
	    			   String currentID = t.getValue("name").toString();	 
	  	        	   Relation circlesMembers = queryingExport.getCirclesFriends(currentID);
		        	   buildNode(circlesMembers, "");
	    	     }
	    		 if (!root.equals("")) 
	    			 xml.append("  </" + root + "> \n");
	    	 }
	    }
}
